/*==============================================
 * Labtask 5a: Testing Terrain.
 *
 * Written by <YOUR NAME HERE>
 *
 *==============================================*/
#pragma once

#include "SceneEngine.h"
#include "Node.h"
#include "GameUtils.h"
#include "Terrain.h"

/** Task 5a: The Terrain
*/
class CTask5a: public CScene
{
	CCameraNode mCamera;
	CTerrain* mpTerrain;

public:
	void Enter();
	void Update(float dt);
	void Draw(float dt);
	void Leave();
};

void CTask5a::Enter()
{
	mpTerrain=new CTerrain(GetDevice(),"media/Terrain/coastMountains.bmp",5,0.5);
	mpTerrain->GenerateTexture(D3DXVECTOR3(0.5f,1,0));
	// setup scene
	SetupDefaultD3DOptions(GetDevice(),true);
	mCamera.Init(D3DXVECTOR3(0,200,0));
	mCamera.LookAt(D3DXVECTOR3(0,0,0));
}
void CTask5a::Update(float dt)
{
	CScene::Update(dt);	// VITAL
	if (CGameWindow::KeyPress(VK_F10))
		ExitScene();

	// basic movement:
	const float SPEED=50.0f;
	const float TURN_SPEED=D2R(45);
	D3DXVECTOR3 move=GetKeyboardVector(WSADRF_KEYS);
	mCamera.Move(move*dt*SPEED);
	D3DXVECTOR3 turn=GetKeyboardVector(CURSOR_HPR_KEYS);
	mCamera.Turn(turn*dt*TURN_SPEED);
}
void CTask5a::Draw(float dt)
{
	mCamera.SetMatrixes(GetDevice());
    // Clear the backbuffer to a black color
    GetDevice()->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, BLUE_COL, 1.0f, 0 );
	GetDevice()->BeginScene();

	// the terrain
	mpTerrain->Draw(IDENTITY_MAT,false);

	GetDevice()->EndScene();
}
void CTask5a::Leave()
{
	SAFE_DELETE(mpTerrain);
}

/** Task 5b: The Putting stuff on the Terrain
*/
class CTask5b: public CScene
{
	CCameraNode mCamera;
	CTerrain* mpTerrain;

	CXMesh* mpCrateMesh;
	CXMesh* mpPlayerMesh;
	CMeshNode mPlayer;

public:
	void Enter();
	void Update(float dt);
	void Draw(float dt);
	void Leave();
};

void CTask5b::Enter()
{
	mpTerrain=new CTerrain(GetDevice(),"media/Terrain/coastMountains.bmp",5,0.5);
	mpTerrain->GenerateTexture(D3DXVECTOR3(0.5f,1,0));
	// setup scene
	SetupDefaultD3DOptions(GetDevice(),true);
	// the models
	mpCrateMesh=new CXMesh(GetDevice(),"media/crate.x");
	mpPlayerMesh=new CXMesh(GetDevice(),"media/teapot.x");
	mPlayer.Init(mpPlayerMesh);
	// make sure its on the ground
	mPlayer.SetPos(mpTerrain->GetPointOnGround(mPlayer.GetPos()));
}
void CTask5b::Update(float dt)
{
	CScene::Update(dt);	// VITAL
	if (CGameWindow::KeyPress(VK_F10))
		ExitScene();

	// basic movement:
	const float SPEED=10.0f;
	D3DXVECTOR3 move=GetKeyboardVector(WSAD_KEYS);
	mPlayer.Move(move*dt*SPEED);
	mPlayer.SetPos(mpTerrain->GetPointOnGround(mPlayer.GetPos(),1.5f));
	mPlayer.SetHpr(mpTerrain->GetOrientationOnGround(mPlayer.GetPos().x,mPlayer.GetPos().z,mPlayer.GetHpr().x));
	// put the camera above it
	mCamera.SetPos(mPlayer.GetPos()+D3DXVECTOR3(0,50,-1));
	mCamera.LookAt(mPlayer);
}
void CTask5b::Draw(float dt)
{
	mCamera.SetMatrixes(GetDevice());
    // Clear the backbuffer to a black color
    GetDevice()->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, BLUE_COL, 1.0f, 0 );
	GetDevice()->BeginScene();

	// the terrain
	mpTerrain->Draw(IDENTITY_MAT,false);
	// the player:
	mPlayer.Draw();
	// a few hundred objects scattered all over the place:
	for(float x=-100;x<100;x+=10)
	{
		for(float z=-100;z<100;z+=10)
		{
			D3DXVECTOR3 pos(x,mpTerrain->GetHeight(x,z),z);
			mpCrateMesh->Draw(pos);
		}
	}
	GetDevice()->EndScene();
}
void CTask5b::Leave()
{
	SAFE_DELETE(mpTerrain);
	SAFE_DELETE(mpCrateMesh);
	SAFE_DELETE(mpPlayerMesh);
}

int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance, 
				   LPTSTR lpCmdLine, int nCmdShow)
{
    ENABLE_LEAK_DETECTION();	// Enable run-time memory check for debug builds.
	CSceneEngine eng(640,480,false);
	eng.AddScene(new CTask5b);
	return eng.Go(hInstance);	// runs everything
}

